package ooww.ssww.normal.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ooww.ssww.strong.junk.ITEMTYPE;
import ooww.ssww.strong.junk.TrustMode;
import java.util.ArrayList;
import java.util.List;

public class CleanTrustDBManager {

    private CleanTrustDBHelper helper;

    public CleanTrustDBManager(Context context) {
        helper = new CleanTrustDBHelper(context);
    }

    /**
     * 数据库添加字段
     */
    public boolean insert(ContentValues values) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        try {
            SQLiteDatabase db = helper.getReadableDatabase();
            long result = db.insert(CleanTrustDBHelper.TABLE_NAME, null, values);
            db.close();
            return result != -1;
        } catch (Exception e) {
        }
        return true;
    }

    /**
     * 查询数据库的总条数
     */
    public int queryCount() {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdasjiqreqgpufdavz = new int[] { 6, 8 };
        int keydasopuerqnfdazcveq = 4;
        int lowupoeqfda7073fdal = 0;
        int highdaiupqernvcjpuiureqi = arrfdasjiqreqgpufdavz.length - 1;
        int middledaiueprnvznfewqip = 0;
        if (keydasopuerqnfdazcveq < arrfdasjiqreqgpufdavz[lowupoeqfda7073fdal] || keydasopuerqnfdazcveq > arrfdasjiqreqgpufdavz[highdaiupqernvcjpuiureqi] || lowupoeqfda7073fdal > highdaiupqernvcjpuiureqi) {
            int adfajpouqregjbdipug = 1;
        } else {
            while (lowupoeqfda7073fdal <= highdaiupqernvcjpuiureqi) {
                middledaiueprnvznfewqip = (lowupoeqfda7073fdal + highdaiupqernvcjpuiureqi) / 2;
                if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] > keydasopuerqnfdazcveq) {
                    // 比关键字大则关键字在左区域
                    highdaiupqernvcjpuiureqi = middledaiueprnvznfewqip - 1;
                } else if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] < keydasopuerqnfdazcveq) {
                    // 比关键字小则关键字在右区域
                    lowupoeqfda7073fdal = middledaiueprnvznfewqip + 1;
                } else {
                }
            }
        }
        int count = 0;
        SQLiteDatabase db = helper.getReadableDatabase();
        String[] columns = { CleanTrustDBHelper.FILE_PATH };
        Cursor cursor = db.query(CleanTrustDBHelper.TABLE_NAME, columns, null, null, null, null, null);
        if (cursor != null) {
            count = cursor.getCount();
            cursor.close();
        }
        db.close();
        return count;
    }

    /**
     * 查询是否包含有指定路径的记录
     *
     * @param path
     * @return
     */
    public boolean isContains(String path) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        boolean contains = false;
        SQLiteDatabase db = helper.getReadableDatabase();
        String[] columns = { CleanTrustDBHelper.FILE_PATH };
        // 查询条件
        String whereClause = CleanTrustDBHelper.FILE_PATH + "=?";
        // 条件参数
        String[] whereArgs = { path };
        Cursor cursor = db.query(CleanTrustDBHelper.TABLE_NAME, columns, whereClause, whereArgs, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            contains = true;
            cursor.close();
        }
        db.close();
        return contains;
    }

    public void delete(String path) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] numbersfdajipureqvna3280 = new int[] { 2, 3 };
        for (int ifdjaiopurq832074 = 0; ifdjaiopurq832074 < numbersfdajipureqvna3280.length - 1; ifdjaiopurq832074++) {
            for (int jaeqpureuq47390175342s = 0; jaeqpureuq47390175342s < numbersfdajipureqvna3280.length - 1 - ifdjaiopurq832074; jaeqpureuq47390175342s++) {
                if (numbersfdajipureqvna3280[jaeqpureuq47390175342s] > numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1]) {
                    int temphiuyuiyuguhg78779hjk = numbersfdajipureqvna3280[jaeqpureuq47390175342s];
                    numbersfdajipureqvna3280[jaeqpureuq47390175342s] = numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1];
                    numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1] = temphiuyuiyuguhg78779hjk;
                }
            }
        }
        SQLiteDatabase db = helper.getReadableDatabase();
        // 删除条件
        String whereClause = CleanTrustDBHelper.FILE_PATH + "=?";
        // 删除条件参数
        String[] whereArgs = { path };
        // 执行删除
        db.delete(CleanTrustDBHelper.TABLE_NAME, whereClause, whereArgs);
        db.close();
    }

    /**
     * 查询对应类型的忽略名单
     *
     * @param it 需要查询的类型 有4中类型，只可以查3中枚举ITEMTYPE的值，如果传入ITEMTYPE.TITLE 会返回size=0的集合
     * @return 返回指定类型的白名单集合
     */
    public ArrayList<TrustMode> query(ITEMTYPE it) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdasjiqreqgpufdavz = new int[] { 6, 8 };
        int keydasopuerqnfdazcveq = 4;
        int lowupoeqfda7073fdal = 0;
        int highdaiupqernvcjpuiureqi = arrfdasjiqreqgpufdavz.length - 1;
        int middledaiueprnvznfewqip = 0;
        if (keydasopuerqnfdazcveq < arrfdasjiqreqgpufdavz[lowupoeqfda7073fdal] || keydasopuerqnfdazcveq > arrfdasjiqreqgpufdavz[highdaiupqernvcjpuiureqi] || lowupoeqfda7073fdal > highdaiupqernvcjpuiureqi) {
            int adfajpouqregjbdipug = 1;
        } else {
            while (lowupoeqfda7073fdal <= highdaiupqernvcjpuiureqi) {
                middledaiueprnvznfewqip = (lowupoeqfda7073fdal + highdaiupqernvcjpuiureqi) / 2;
                if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] > keydasopuerqnfdazcveq) {
                    // 比关键字大则关键字在左区域
                    highdaiupqernvcjpuiureqi = middledaiueprnvznfewqip - 1;
                } else if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] < keydasopuerqnfdazcveq) {
                    // 比关键字小则关键字在右区域
                    lowupoeqfda7073fdal = middledaiueprnvznfewqip + 1;
                } else {
                }
            }
        }
        ArrayList<TrustMode> list = new ArrayList<TrustMode>();
        if (it == ITEMTYPE.TITLE) {
            return null;
        }
        int t = it.ordinal();
        SQLiteDatabase db = helper.getReadableDatabase();
        String[] columns = { CleanTrustDBHelper.FILE_NAME, CleanTrustDBHelper.FILE_PATH, CleanTrustDBHelper.FILE_SIZE, CleanTrustDBHelper.FILE_TYPE };
        Cursor cursor = db.query(CleanTrustDBHelper.TABLE_NAME, columns, CleanTrustDBHelper.FILE_TYPE + "=?", new String[] { t + "" }, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String name = cursor.getString(0);
                String path = cursor.getString(1);
                long size = cursor.getLong(2);
                // int type = cursor.getInt(3);
                TrustMode mode = new TrustMode();
                mode.name = name;
                mode.path = path;
                mode.size = size;
                // ITEMTYPE.values()[type];//把int值转换成对应的枚举类型
                mode.type = it;
                list.add(mode);
            }
            cursor.close();
        }
        db.close();
        return list;
    }

    /**
     * 查询全部数据
     * 返回以hashmap的形式返回
     */
    public List<TrustMode> queryAll() {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] numbersfdajipureqvna3280 = new int[] { 2, 3 };
        for (int ifdjaiopurq832074 = 0; ifdjaiopurq832074 < numbersfdajipureqvna3280.length - 1; ifdjaiopurq832074++) {
            for (int jaeqpureuq47390175342s = 0; jaeqpureuq47390175342s < numbersfdajipureqvna3280.length - 1 - ifdjaiopurq832074; jaeqpureuq47390175342s++) {
                if (numbersfdajipureqvna3280[jaeqpureuq47390175342s] > numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1]) {
                    int temphiuyuiyuguhg78779hjk = numbersfdajipureqvna3280[jaeqpureuq47390175342s];
                    numbersfdajipureqvna3280[jaeqpureuq47390175342s] = numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1];
                    numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1] = temphiuyuiyuguhg78779hjk;
                }
            }
        }
        List<TrustMode> result = new ArrayList<TrustMode>();
        ITEMTYPE[] types = ITEMTYPE.values();
        for (int i = 0; i < types.length; i++) {
            ITEMTYPE itemtype = types[i];
            // 如果是title 跳出本次循环
            if (itemtype == ITEMTYPE.TITLE)
                continue;
            List<TrustMode> data = query(types[i]);
            if (data != null && data.size() > 0) {
                TrustMode mode = new TrustMode();
                switch(itemtype) {
                    case CACHE:
                        mode.name = "缓存";
                        break;
                    case REMAIN:
                        mode.name = "残留文件";
                        break;
                    case APKFILE:
                        mode.name = "安装包";
                }
                mode.type = ITEMTYPE.TITLE;
                mode.count = data.size();
                result.add(mode);
                result.addAll(data);
            }
        }
        return result;
    }
}
